package com.example.lx5_22.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.base.entity.ParkEntity
import com.example.base.entity.ParkEntityItem
import com.example.base.mvvm.BaseViewModel
import com.example.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService) : BaseViewModel() {

    fun getPark() : Flow<PagingData<ParkEntityItem>>{
//        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
//            ParkPagingSource(apiService)
//        }).flow.cachedIn(viewModelScope)
        return httpRequestFlow {
            ParkPagingSource(apiService)
        }
    }

    val addParkData = MutableLiveData<ParkEntityItem>()
    fun getAddPark(item : ParkEntityItem){
        httpRequest({apiService.getAddPark(toBody(item))},addParkData::postValue)
    }

    val queryParkData = MutableLiveData<ParkEntityItem>()
    fun getQueryParkData(id:Int,status:String){
        httpRequest({apiService.getQuery(id, status)},queryParkData::postValue)
    }

    val deleteParkData = MutableLiveData<Boolean>()
    fun getDeletePark(id:Int){
        httpRequest({apiService.getDelete(id)},deleteParkData::postValue)
    }

}